package com.javaforever.clocksimplejee4.facade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baidu.ai.aip.utils.Base64Util;
import com.javaforever.clocksimplejee4.domain.User;
import com.javaforever.clocksimplejee4.service.UserService;
import com.javaforever.clocksimplejee4.serviceimpl.FaceServiceImpl;
import com.javaforever.clocksimplejee4.serviceimpl.UserServiceImpl;

import net.sf.json.JSONObject;

public class RegisterUserFacade extends HttpServlet {

       protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new TreeMap<String,Object>();
        try {
        	String imageData = request.getParameter("image");
        	UserService userService = new UserServiceImpl();
        	FaceServiceImpl fimpl = new FaceServiceImpl();
        	User u = userService.findUserByFace(imageData);
        	
        	if (fimpl.detectOneValidFace(Base64Util.decode(imageData))){
	        	if (u==null||u.getScore()<78d) {
		            User user = new User();
		            user.setEmpid(Long.parseLong(request.getParameter("empid")));
		            user.setUsername(request.getParameter("username").trim());
		            user.setFirstname(request.getParameter("username").trim());
		            user.setLastname(request.getParameter("lastname").trim());
		            user.setSex(request.getParameter("sex").trim());
		            user.setPassword(request.getParameter("password").trim());
		            user.setConfirmpassword(request.getParameter("confirmpassword").trim());
		            user.setNamec(request.getParameter("namec").trim());
		            user.setNamej(request.getParameter("namej").trim());
		            user.setAddress(request.getParameter("address").trim());
		            user.setAddress1(request.getParameter("address1").trim());
		            user.setPhone(request.getParameter("phone").trim());
		            user.setMobile(request.getParameter("mobile").trim());
		            user.setVip(false);
		            user.setFaceUid(user.getUsername());
		            user.setImageData1(request.getParameter("image"));
		
		            
		            boolean success = userService.registerUser(user);
		            
		            result.put("success", success);
		            result.put("data", user);
		            out.println(JSONObject.fromObject(result));
		            return;
	        	} else {
	        		result.put("success", false);
	 	            result.put("errorMessage", "User already exists.");
	 	            out.println(JSONObject.fromObject(result));
	 	            return;
	        	}        	
        	}else {
        		result.put("success", false);
 	            result.put("errorMessage", "Must has one and only one human face.");
 	            out.println(JSONObject.fromObject(result));
 	            return;
        	}
        } catch (Exception e){
        	e.printStackTrace();
        	result.put("success", false);
            result.put("errorMessage", "Must has one and only one human face.");
            out.println(JSONObject.fromObject(result));
         } finally {
            out.close();
        }
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}



